<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class repair_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();

	}
	public function get_view($id)
	{
	$this->datatables
			->select("customer.id as id, case.case_no,case.hp_case,repair.assigned_date,repair.expected_date,status.name,repair.docket_no,customer.customer_name,customer.mobile")
			->from('repair','customer','users','status','case')
			->join('case','repair.case_id=case.id')
			->join('customer','case.service_id=customer.service_id')
			->join('users','users.id=repair.engineer_id')
			->join('status','repair.status_id=status.id')
		    ->where('repair.status_id',5)
			->where('users.id',$id)
		    ->add_column("Actions",
			"<center>		
								<a href='index.php/engineer/repair/view_detail/$1'>
								<i class='icon-edit'></i>
								</a
								
								</center>", "id")
			
			;
	   return  $this->datatables->generate();
	}
	public function get_view_nonwarranty($id)
	{
	$this->datatables
			->select("customer.id as id, nonwarranty_case.case_no,nonwarranty_repair.order_no,nonwarranty_repair.assigned_date,nonwarranty_repair.expected_date,status_nonwarranty.name,customer.customer_name,customer.mobile")
			->from('nonwarranty_repair','customer','users','status_nonwarranty','nonwarranty_case')
			->join('nonwarranty_case','nonwarranty_repair.case_id=nonwarranty_case.id')
			->join('customer','nonwarranty_case.service_id=customer.service_id')
			->join('users','users.id=nonwarranty_repair.engineer_id')
			->join('status_nonwarranty','nonwarranty_repair.status_id=status_nonwarranty.id')
		    ->where('nonwarranty_repair.status_id',6)
			->where('users.id',$id)
		    ->add_column("Actions",
			"<center>		
								<a href='index.php/engineer/repair/view_detail_nonwarranty/$1'>
								<i class='icon-edit'></i>
								</a
								
								</center>", "id")
			
			;
	   return  $this->datatables->generate();
	}
public function get_cus($id)
	{
	$query = $this->db->get_where('customer', array('id' => $id));
		return $query;
	}
public function get_case($id)
{
$this->db->select('case.case_no,case.hp_case,case.id,repair.docket_no');
	$this->db->from('case', 'repair','customer' );
	$this->db->join('customer','customer.service_id=case.service_id');
	$this->db->join('repair', 'repair.case_id= case.id  '  );
	$this->db->where('customer.id',$id);
	$this->db->where('case.status_id != 22');
	$q = $this->db->get();
	return $q;
		
}
public function get_case_nonwarranty($id)
{
$this->db->select('nonwarranty_case.case_no,nonwarranty_case.id,nonwarranty_repair.order_no');
	$this->db->from('nonwarranty_case', 'nonwarranty_repair','customer' );
	$this->db->join('customer','customer.service_id=nonwarranty_case.service_id');
	$this->db->join('nonwarranty_repair', 'nonwarranty_repair.case_id= nonwarranty_case.id  '  );
	$this->db->where('customer.id',$id);
	$q = $this->db->get();
	return $q;
		
}
public function get_repair_detail($id)
{
$query = $this->db->get_where('repair', array('case_id' => $id));
		return $query;

}
public function nonwarranty_repair($id)
{
$query = $this->db->get_where('nonwarranty_repair', array('case_id' => $id));
		return $query;

}
public function get_docket($id)
{
$this->db->select('case.case_no,case.id,repair.docket_no');
	$this->db->from('case', 'repair','customer' );
	$this->db->join('customer','customer.service_id=case.service_id');
	$this->db->join('repair', 'repair.case_id= case.id  '  );
	$this->db->where('case.id',$id);
	$q = $this->db->get();
	return $q;
		
}
public function get_order($id)
{
$this->db->select('nonwarranty_case.case_no,nonwarranty_case.id,nonwarranty_repair.order_no');
	$this->db->from('nonwarranty_case', 'nonwarranty_repair' );
	$this->db->join('nonwarranty_repair', 'nonwarranty_repair.case_id= nonwarranty_case.id  '  );
	$this->db->where('nonwarranty_case.id',$id);
	$q = $this->db->get();
	return $q;
		
}

public function update_repair($id,$insert)
{

$this->db->update('repair',$insert,array('case_id'=>$id));
}
public function update_nonwarranty_repair($id,$insert)
{

$this->db->update('nonwarranty_repair',$insert,array('case_id'=>$id));


}
public function update_case_accept($id,$insert)
{
$this->db->update('case',$insert,array('id'=>$id,'status_id'=>5,));
}
public function update_nonwarrantycase_accept($id,$insert)
{

$this->db->update('nonwarranty_case',$insert,array('id'=>$id));


}
public function get_current_view($id)
	{
$this->datatables
			->select("customer.id as id, case.case_no,case.hp_case,repair.assigned_date,repair.expected_date,status.name,repair.docket_no,customer.customer_name,customer.mobile")
			->from('repair','customer','users','status','case')
			->join('case','repair.case_id=case.id')
			->join('customer','case.service_id=customer.service_id')
			->join('users','users.id=repair.engineer_id')
			->join('status','repair.status_id=status.id')
			->where('case.status_id != 22')
			->where('users.id',$id)
		    ->where( 'repair.status_id BETWEEN 6 AND 9') 
		   
			
		    ->add_column("Actions",
			"<center>		<a href='index.php/engineer/repair/part_verification/$1'>
								<i class='icon-tags '></i>
								</a>
								
								</center>", "id")
			
			;
			
	   return  $this->datatables->generate();
	}
	public function get_currentnonwarranty_view($id)
	{
$this->datatables
			->select("customer.id as id, nonwarranty_case.case_no,nonwarranty_repair.order_no,nonwarranty_repair.assigned_date,nonwarranty_repair.expected_date,status_nonwarranty.name,customer.customer_name,customer.mobile")
			->from('nonwarranty_repair','customer','users','status_nonwarranty','nonwarranty_case')
			->join('nonwarranty_case','nonwarranty_repair.case_id=nonwarranty_case.id')
			->join('customer','nonwarranty_case.service_id=customer.service_id')
			->join('users','users.id=nonwarranty_repair.engineer_id')
			->join('status_nonwarranty','nonwarranty_repair.status_id=status_nonwarranty.id')
			->where('users.id',$id)
		    ->where( 'nonwarranty_repair.status_id BETWEEN 7 AND 10') 
		   
			
		    ->add_column("Actions",
			"<center>		<a href='index.php/engineer/repair/nonwarranty_part_verification/$1'>
								<i class='icon-tags '></i>
								</a>
								
								
								
								</center>", "id")
			
			;
			
	   return  $this->datatables->generate();
	}
	
public function get_so($case_id)
{
$query = $this->db->get_where('so', array('case_id' => $case_id));
		return $query->result();
}
public function get_part($case_id)
{
$this->db->select('received_order_part.name,received_order_part.id,received_order_part.model,received_order_part.manufacturing_date,received_order_part.price,received_order_part.serial');
	$this->db->from('received_order', 'received_order_part' );
	$this->db->join('received_order_part', 'received_order.id= received_order_part.received_order_id'  );
	$this->db->where('received_order.case_id',$case_id);
	$q = $this->db->get();
	return $q->result();
}

public function send_defective($data1)
{
$this->db->insert('defective',$data1);
return $this->db->insert_id();

}
public function send_defective_part($data)
{
$this->db->insert('defective_part',$data);
}



public function insert($userfile,$so_id,$case_id){	
$insert=array('so_id'=>$so_id,
              'case_id' =>$case_id,
			  'image' =>$userfile,);
			
$this->db->insert('oldpart_img',$insert);
			
}
public function insert_new($userfile,$so_id,$case_id){	

$insert=array('so_id'=>$so_id,
              'case_id' =>$case_id,
			  'image' =>$userfile,);
			
$this->db->insert('newpart_img',$insert);
			
}
public function update_current_status($id)
{
$insert=array('status_id'=>9,);

$this->db->update('repair',$insert,array('case_id'=>$id));
}
public function nonwarranty_current_status($id)
{
$insert=array('status_id'=>10,);

$this->db->update('nonwarranty_repair',$insert,array('case_id'=>$id));
}
public function update_current_case($id)
{
$insert=array('status_id'=>9,);

$this->db->update('case',$insert,array('id'=>$id));
}
public function nonwarranty_current_case($id)
{
$insert=array('status_id'=>10,);

$this->db->update('nonwarranty_case',$insert,array('id'=>$id));
}
public function update_defective_status($id)
{
$insert=array('status_id'=>8,);

$this->db->update('repair',$insert,array('case_id'=>$id));
}
public function update_defective_case($id)
{
$insert=array('status_id'=>8,);

$this->db->update('case',$insert,array('id'=>$id));
}
public function update_verification($id)
{
$insert=array('status_id'=>7,);

$this->db->update('repair',$insert,array('case_id'=>$id));
}
public function update_verification_nonwarranty($id)
{
$insert=array('status_id'=>8,);

$this->db->update('nonwarranty_repair',$insert,array('case_id'=>$id));
}
public function update_case_verification($id)
{
$insert=array('status_id'=>7,);

$this->db->update('case',$insert,array('id'=>$id));
}
public function update_nonwarranty_case_verification($id)
{
$insert=array('status_id'=>8,);

$this->db->update('nonwarranty_case',$insert,array('id'=>$id));
}
public function update_upload_status($id)
{
$insert=array('status_id'=>10,'completion_date'=>date('Y-m-d'),);

$this->db->update('repair',$insert,array('case_id'=>$id));

}
public function update_case_upload($id)
{
$insert=array('status_id'=>10,);

$this->db->update('case',$insert,array('id'=>$id));

}
public function get_all_view($id)
	{
$this->datatables
			->select("customer.id as id, case.case_no,case.hp_case,repair.assigned_date,repair.completion_date,status.name,repair.docket_no,customer.customer_name,customer.mobile")
			->from('repair','customer','users','status','case')
			->join('case','repair.case_id=case.id')
			->join('customer','case.service_id=customer.service_id')
			->join('users','users.id=repair.engineer_id')
			->join('status','repair.status_id=status.id')
			->where('users.id',$id)
		    ->where( 'repair.status_id', 10) 
		   
			
		    ->add_column("Actions",
			"<center>		<a href='index.php/engineer/repair/view_detail_completed/$1'>
								<i class='icon-tags'></i>
								</a>
								<a href='index.php/engineer/repair/enter_expenses/$1'>
								<i class='icon-edit'></i>
								</a>
								
								
								</center>", "id")
			
			;
			
	   return  $this->datatables->generate();
	}
	public function nonwarranty_getall($id)
	{
$this->datatables
			->select("customer.id as id, nonwarranty_case.case_no,nonwarranty_repair.assigned_date,nonwarranty_repair.completion_date,status_nonwarranty.name,nonwarranty_repair.order_no,customer.customer_name,customer.mobile")
			->from('nonwarranty_repair','customer','users','status_nonwarranty','nonwarranty_case')
			->join('nonwarranty_case','nonwarranty_repair.case_id=nonwarranty_case.id')
			->join('customer','nonwarranty_case.service_id=customer.service_id')
			->join('users','users.id=nonwarranty_repair.engineer_id')
			->join('status_nonwarranty','nonwarranty_repair.status_id=status_nonwarranty.id')
			->where('users.id',$id)
		    ->where( 'nonwarranty_repair.status_id', 11) 
		   
			
		    ->add_column("Actions",
			"<center>		<a href='index.php/engineer/repair/nonwarranty_completed/$1'>
								<i class='icon-tags'title='View detail'></i>
								</a>
								<a href='index.php/engineer/repair/enter_nonwarranty_expenses/$1'>
								<i class='icon-edit' tgitle='Expenses'></i>
								</a>
								
								
								</center>", "id")
			
			;
			
	   return  $this->datatables->generate();
	}
public function get_image($id)
{

$query = $this->db->get_where('oldpart_img', array('so_id' => $id));
		return $query->result();
		
}
public function get_imagenew($id)
{

$query = $this->db->get_where('newpart_img', array('so_id' => $id));
		return $query->result();
		
}


public function count_old($id)
{
$query = $this->db->get_where('oldpart_img', array('so_id' => $id));
return count($query->id);

}

public function get_service_id($id)
{
$query = $this->db->get_where('case', array('id' => $id));
return $query;



}
public function get_caseid()
	{
	$this->db->select_max('id');
    return $this->db->get('case');
	}	
public function create_newcase($insert)
{

$this->db->insert('case',$insert);
return  $this->db->insert_id();

}

public function reassigned_case($insert)
{
$this->db->insert('reassigned_caseid',$insert);


}
public function update_repair_final($id,$report)
{
$insert=array('report'=>$report,'completion_date'=>date('y/m/d'),'status_id'=>11,);
$this->db->update('nonwarranty_repair',$insert,array('case_id'=>$id));

}
public function update_case_final($id)
{
$insert=array('status_id'=>11,);
$this->db->update('nonwarranty_case',$insert,array('id'=>$id));

}
public function get_defective_so($id)
{
$this->db->select('so.id,defective.docket_no,so.name,so.code,so.so_no');
	$this->db->from('so', 'defective','defective_part' );
	$this->db->join('defective','defective.case_id=so.case_id');
	$this->db->join('defective_part', 'defective.id=defective_part.defective_id ');
	$this->db->where('defective.case_id',$id);
	$q = $this->db->get();
	return $q->result();
		
}
public function redispatch_so($data)
{
$this->db->insert('redispatch_so',$data);

}
public function delete_repair($id)
{
$this->db->delete('repair',array('case_id'=>$id));

}
public function update_statuscase($id)
{
$insert=array('status_id'=>16,);
$this->db->update('case',$insert,array('id'=>$id));

}
public function insert_expense($expense)
{
$this->db->insert('engineer_expenses',$expense);
}
public function insert_expense_nonwarranty($expense)
{
$this->db->insert('nonwarranty_engineer_expenses',$expense);
}
public function get_expense_detail($case_id)
{
$query = $this->db->get_where('engineer_expenses', array('case_id' => $case_id));
return $query;
}  
public function get_nonwarranty_expense_detail($case_id)
{
$query = $this->db->get_where('nonwarranty_engineer_expenses', array('case_id' => $case_id));
return $query;
}  
public function update_expenses_engineer($insert,$case_id)
{
$expense= $this->db->get_where('engineer_expenses', array('case_id' => $case_id))->num_rows();
if($expense != 0)
{
$this->db->update('engineer_expenses',$insert,array('case_id'=>$case_id));

}
else
      {
       $this->db->insert('engineer_expenses',$insert);
          }
}



	
	}
	?>